package com.haotunet.app.goldtreasure.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.WithdrawalsRcdAdapter;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.WithDrawRecordInfo;
import com.haotunet.app.goldtreasure.model.WithDrawRecordModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.TitleView;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout.OnRefreshListener;
import com.haotunet.app.pulltorefresh.pullableView.PullableListView;

import java.util.List;

@Route(path = "/mine/redraw_record")
public class WithdrawalsRecordAct extends BaseActivity {
    private TitleView mTitleView;
    private LinearLayout noRecordLayout;
    private PullableListView lvRerord;
    private PullToRefreshLayout mfreshLayout;
    private int pageNum = 20;
    private final int GET_WITHDRAW_RECORD_SUCCESS = 2;
    private final int LOAD_MORE_WITHDRAW_RECORD_SUCCESS = 3;
    private Handler handle = new Handler() {

        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case GET_WITHDRAW_RECORD_SUCCESS:
                    WithDrawRecordModel model = (WithDrawRecordModel) msg.obj;
                    List<WithDrawRecordInfo> list = model.getItems();
                    if (list.size() == 0) {
                        noRecordLayout.setVisibility(View.VISIBLE);
                        return;
                    }
                    withdrawalsRcdAdapter = new WithdrawalsRcdAdapter(WithdrawalsRecordAct.this, list);
                    withdrawalsRcdAdapter.setDatas(list);
                    lvRerord.setAdapter(withdrawalsRcdAdapter);
                    break;
                case LOAD_MORE_WITHDRAW_RECORD_SUCCESS:
                    WithDrawRecordModel moreModel = (WithDrawRecordModel) msg.obj;
                    withdrawalsRcdAdapter.addDatas(moreModel.getItems());
                    if (moreModel.getItems().size() == 0) {
                        UIHelper.showToast(WithdrawalsRecordAct.this, getString(R.string.last_page_hint));
                    }
                    break;
                default:
                    break;
            }
        }

    };
    private WithdrawalsRcdAdapter withdrawalsRcdAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdrawals_record);
        initView();
        initData("0", "");
    }

    private void initData(String pagenum, final String action) {
        ActionUtil actionUtil = new ActionUtil(WithdrawalsRecordAct.this);
        actionUtil.getWithdrawRecord(pagenum);
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()){
                    return;
                }
                WithDrawRecordModel withdrawModel = (WithDrawRecordModel) model;
                if (withdrawModel == null) {
                    UIHelper.showToast(WithdrawalsRecordAct.this, getString(R.string.rank_fail));
                    return;
                }
                if (withdrawModel.getErrorMsg() != null) {
                    UIHelper.showToast(WithdrawalsRecordAct.this, withdrawModel.getErrorMsg());
                } else {
                    if (action.equals("refresh") || action.equals("loadmore")) {
                        mfreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                    Message msg = new Message();
                    if (action.equals("loadmore")) {
                        msg.what = LOAD_MORE_WITHDRAW_RECORD_SUCCESS;
                        pageNum = pageNum + 20;
                    } else {
                        msg.what = GET_WITHDRAW_RECORD_SUCCESS;
                    }
                    msg.obj = withdrawModel;
                    handle.sendMessage(msg);
                }
            }
        });
    }

    private void initView() {
        mTitleView = (TitleView) findViewById(R.id.title_view);
        mTitleView.getTitletextView().setVisibility(View.VISIBLE);
        mTitleView.getTitletextView().setText("提现记录");
        mTitleView.getBackView().setVisibility(View.VISIBLE);
        mTitleView.getBackView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        noRecordLayout = (LinearLayout) findViewById(R.id.no_record_layout);
        mfreshLayout = (PullToRefreshLayout) findViewById(R.id.id_swipe_ly);
        lvRerord = (PullableListView) findViewById(R.id.lv_withdrawals_record);
        RefreshData(0);
    }

    private void RefreshData(final int pagenum) {
        mfreshLayout.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
                initData(pagenum + "", "refresh");
                pageNum = 20;
            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
                initData(pageNum + "", "loadmore");
            }
        });
    }

}
